package com.android.qjpmc.application_module.pop;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.android.qjpmc.R;
import com.android.qjpmc.application_module.home.model.StationDataModel;
import com.android.qjpmc.bean.PointParameterBean;
import com.android.qjpmc.bean.PwrDeviceBean;
import com.android.qjpmc.databinding.ItemPopDevicesBinding;
import com.android.qjpmc.databinding.PopDevicesPointListBinding;
import com.android.qjpmc.utils.CopyUtils;
import com.android.qjpmc.widget.FlowLayoutManager;
import com.android.qjpmc.widget.base.BasePopupWindow;
import com.android.qjpmc.widget.databindingadapter.BaseBindingItemPresenter;
import com.android.qjpmc.widget.databindingadapter.BaseDataBindingDecorator;
import com.android.qjpmc.widget.databindingadapter.SingleTypeBindingAdapter;
import com.android.qjpmc.widget.retrofithelper.rxsubscriber.ProgressObserver;
import com.android.qjpmc.widget.utils.ToastUtils;
import com.android.qjpmc.widget.utils.UIUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import io.reactivex.annotations.NonNull;

public class DevicesPointListPopupWindow extends BasePopupWindow<PopDevicesPointListBinding> implements BaseBindingItemPresenter<PwrDeviceBean> {

    private SingleTypeBindingAdapter adapter;
    private List<PwrDeviceBean> devicesList;
    private SingleTypeBindingAdapter adapterChild;
    private DevicesPointListChooseListener devicesPointListChooseListener;
    int devicesSelectPosition = 0;
    StationDataModel stationDataModel = new StationDataModel();
    private SingleTypeBindingAdapter chooseDevicesPointTagAdapter;

    public DevicesPointListPopupWindow(FragmentActivity activity) {
        super(activity);
    }

    @Override
    public int getLayoutId() {
        return R.layout.pop_devices_point_list;
    }

    @Override
    public void initView() {
        initRecycler(devicesList);
    }


    private void initRecycler(List<PwrDeviceBean> devicesList) {
        mBinding.setView(this);
        initCategoryRecyclerView(devicesList, devicesSelectPosition, true);
        getPointListOfGroup(devicesList.get(devicesSelectPosition), 0);
        buildSelectUI();
    }

    private void initCategoryRecyclerView(List<PwrDeviceBean> devicesList, int categorySelectPosition, boolean isInit) {
        this.devicesList = devicesList;
        devicesList.get(categorySelectPosition).isOpen = true;
        if (isInit) {
            adapter = new SingleTypeBindingAdapter(UIUtils.getContext(), devicesList, R.layout.item_pop_devices);
            mBinding.recyclerViewCategory.setAdapter(adapter);
            adapter.setItemPresenter(this);
            mBinding.recyclerViewCategory.setLayoutManager(new LinearLayoutManager(activitySoftReference.get()));
            adapter.setItemDecorator(new BaseDataBindingDecorator<PwrDeviceBean, ItemPopDevicesBinding>() {
                @Override
                public void decorator(ItemPopDevicesBinding binding, int position, int viewType, PwrDeviceBean data) {
                    if (data.isOpen) {
                        binding.llContent.setBackgroundResource(R.color.white);
                    } else {
                        binding.llContent.setBackgroundResource(R.color.gray_f8);
                    }
                }
            });
        } else {
            adapter.refreshNotClear(devicesList);
        }


    }

    private void initChildRecycler(@NonNull PwrDeviceBean deviceBean, boolean isInit, int parentPosition) {
        if (deviceBean.pointList == null) {
            return;
        }
        List<PointParameterBean> list = deviceBean.pointList;
        if (adapterChild == null) {
            adapterChild = new SingleTypeBindingAdapter(UIUtils.getContext(), list, R.layout.item_pop_devices_child);
            mBinding.recyclerViewChild.setAdapter(adapterChild);
            adapterChild.setItemPresenter(this);
            mBinding.recyclerViewChild.setLayoutManager(new LinearLayoutManager(activitySoftReference.get()));
        } else {
            adapterChild.refreshNotClear(list);
        }

    }

    @Override
    public void initData() {
        // mPopupWindow.setAnimationStyle(R.style.popwindow_anim);
    }

    @Override
    public void initEvent() {

    }


    @Override
    public void onItemClick(int itemPosition, PwrDeviceBean itemData) {
        if (itemData.isOpen) return;
        List<PwrDeviceBean> listData = adapter.getListData();
        devicesSelectPosition = itemPosition;
        for (PwrDeviceBean listDatum : listData) {
            listDatum.isOpen = false;
        }
        itemData.isOpen = true;
        if (itemData.pointList == null) {
            getPointListOfGroup(itemData, itemPosition);
        } else {
            initChildRecycler(itemData, false, itemPosition);
        }
        adapter.notifyDataSetChanged();
    }

    private void getPointListOfGroup(PwrDeviceBean data, int itemPosition) {
        stationDataModel.getPointListOfGroup(data.groupId, 1).subscribe(new ProgressObserver<List<PointParameterBean>>(false, null) {
            @Override
            public void _onNext(List<PointParameterBean> groupPointList) {
                data.pointList = groupPointList;
                initChildRecycler(data, false, itemPosition);
            }
        });

    }

    public void onItemChildClick(int itemPosition, PointParameterBean itemData) {
        itemData.isCheck = !itemData.isCheck;
        adapterChild.refresh(itemPosition);
        buildSelectUI();

    }

    public void onDeletePoint(int itemPosition, PointParameterBean deletePointParameter) {
        List<PointParameterBean> listData = chooseDevicesPointTagAdapter.getListData();
        for (PwrDeviceBean deviceBean : devicesList) {
            if (deviceBean.pointList != null) {
                for (PointParameterBean pointParameterBean : deviceBean.pointList) {
                    if (pointParameterBean.propsCode.equals(deletePointParameter.propsCode)) {
                        pointParameterBean.isCheck = false;
                        listData.remove(deletePointParameter);
                    }
                }
            }
        }
        chooseDevicesPointTagAdapter.refreshNotClear(listData);
        adapterChild.notifyDataSetChanged();

    }

    private void buildSelectUI() {
        List<PointParameterBean> pointCodeList = new ArrayList<>();
        for (PwrDeviceBean pwrDeviceBean : devicesList) {
            if (pwrDeviceBean.pointList != null) {
                for (PointParameterBean groupPointListBean : pwrDeviceBean.pointList) {
                    groupPointListBean.deviceName = pwrDeviceBean.deviceName;
                    if (groupPointListBean.isCheck) {
                        pointCodeList.add(0, groupPointListBean);
                    }
                }
            }
        }
        if (pointCodeList.size() > 0) {
            if (chooseDevicesPointTagAdapter == null) {
                mBinding.selectPointRecycler.setLayoutManager(new FlowLayoutManager());
                chooseDevicesPointTagAdapter = new SingleTypeBindingAdapter(activitySoftReference.get(), null, R.layout.item_choose_devices_point_tag);
                chooseDevicesPointTagAdapter.setItemPresenter(this);
                mBinding.selectPointRecycler.setAdapter(chooseDevicesPointTagAdapter);
            } else {
                chooseDevicesPointTagAdapter.refreshNotClear(pointCodeList);
            }
            mBinding.llSelectedPoint.setVisibility(View.VISIBLE);
        } else {

            mBinding.llSelectedPoint.setVisibility(View.INVISIBLE);
        }

    }

    public void initDevicesListData(List<PwrDeviceBean> devicesList) {
        this.devicesList = devicesList;
    }

    public void setDevicesPointListChooseListener(DevicesPointListChooseListener courseSearchPopFilterListener) {
        this.devicesPointListChooseListener = courseSearchPopFilterListener;
    }

    boolean isReset = false;

    @Override
    public void onShowPopupWindow() {
        super.onShowPopupWindow();
        isReset = false;
    }

    @Override
    public void onDismissPopupWindow() {
        super.onDismissPopupWindow();
        isReset = false;
    }

    public void onEmptyMethod() {
    }

    public void onReset() {
        isReset = true;
        List<PwrDeviceBean> deviceList = CopyUtils.deepCopyList(devicesList);
        for (PwrDeviceBean deviceBean : deviceList) {
            deviceBean.isOpen = false;
            for (PointParameterBean child : deviceBean.pointList) {
                child.isCheck = false;
            }
        }
        initCategoryRecyclerView(deviceList, 0, false);
        adapterChild.refreshNotClear(deviceList.get(0).pointList);

    }

    public void onConfirm() {
        List<PointParameterBean> pointCodeList = new ArrayList<>();
        for (PwrDeviceBean pwrDeviceBean : devicesList) {
            if (pwrDeviceBean.pointList != null) {
                for (PointParameterBean groupPointListBean : pwrDeviceBean.pointList) {
                    groupPointListBean.deviceName = pwrDeviceBean.deviceName;
                    if (groupPointListBean.isCheck) {
                        pointCodeList.add(groupPointListBean);
                    }
                }
            }
        }
        if (pointCodeList.size() <= 0) {
            ToastUtils.showShort("请选择设备点");
            return;
        }
        if (pointCodeList.size() > 6) {
            ToastUtils.showShort("最多只能选择6条");
            return;
        }
        isReset = false;
        if (devicesPointListChooseListener != null) {
            devicesPointListChooseListener.onPopChooseResult(pointCodeList);
        }
        dismiss();
    }


    public interface DevicesPointListChooseListener {

        void onPopChooseResult(List<PointParameterBean> pointCodeList);
    }


}


